package com.drive.service.serviceimpl;

import com.drive.bean.OrderSetting;
import com.drive.mapper.OrderSettingMapper;
import com.drive.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 往
 * 预约设置接口实现类
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    //批量添加数据
    @Override
    public void add(List<OrderSetting> orderSettingsList) {
        if (orderSettingsList!=null&&orderSettingsList.size()>0){
            //遍历集合
            for (OrderSetting orderSetting : orderSettingsList) {
                //检查此数据是否存在
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    //存在数据，执行更新操作
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }else {
                    //没有数据，执行添加操作
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";//2019-3-1
        String dateEnd = date + "-31";//2019-3-31
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询当前日期是否进行了预约设置
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //当前日期进行了预约设置，进行修改
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else {
            //当前日期为进行修改，添加
            orderSettingMapper.add(orderSetting);
        }
    }
}
